using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MySql.Data.MySqlClient;
namespace ChuanPlus.FormControl
{
    public partial class ControlDebtor : DevExpress.XtraEditors.XtraUserControl
    {
        DataTable m_dtDebtor = null;
        double m_dOpeningBalance = 0;
        Utils.clsReport exportReport = new ChuanPlus.Utils.clsReport();
        public ControlDebtor()
        {
            InitializeComponent();
            comboCustomer.Properties.Items.AddRange(xpColCustomer);           
        }
        
        private void buttonProcess_Click(object sender, EventArgs e)
        {
            if (validationForm())
            {
                m_dtDebtor = ChuanPlus.DAO.ReceiptVoucherDAO.GetDebtorReport((comboCustomer.SelectedItem as Customer).CustomerID, dateFrom.DateTime, dateTo.DateTime);
                grid.DataSource = m_dtDebtor;
                m_dOpeningBalance = ChuanPlus.DAO.ReceiptVoucherDAO.GetOpeningDebtorBalance((comboCustomer.SelectedItem as Customer).CustomerID, dateFrom.DateTime);
                lblOpeningBalance.Text = string.Format("{0:n2} VND", m_dOpeningBalance);
                double total = Convert.ToDouble(gridView1.Columns["Balance"].SummaryItem.SummaryValue);
                double closingBalance = m_dOpeningBalance + total;
                lblClosingBalance.Text = string.Format("{0:n2} VND", closingBalance); 
            }
        }

        private void buttonExport_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_dtDebtor != null)
                    exportReport.ExportDebtorReport(m_dtDebtor, dateFrom.DateTime, dateTo.DateTime, comboCustomer.SelectedItem as Customer, m_dOpeningBalance);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region validaition
        private void comboBoCustomer_Validating(object sender, CancelEventArgs e)
        {
            Customer c = comboCustomer.SelectedItem as Customer;
            if (c == null)
            {
                e.Cancel = true;
                comboCustomer.ErrorText = "Customer " + comboCustomer.Text + " does not exsit";
            }
        }
        private bool validationForm()
        {
            string errorObj = "";
            bool valid = true;
            if (dateFrom.Text == "")
            {
                errorObj = " From Date "; valid = false;
            }
            else if (dateTo.Text == "")
            {
                errorObj = " To Date "; valid = false;
            }
            else if (comboCustomer.Text == "")
            {
                errorObj = " Customer "; valid = false;
            }
            if (!valid)
                MessageBox.Show("Please enter " + errorObj, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return valid;
        }
        #endregion
    }
}
